package com.mashang.servlet.stu;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mashang.common.PageVo;
import com.mashang.service.StudentService;
import com.mashang.servlet.clazz.ClazzAllListServlet;
import com.mashang.domain.Clazz;
import com.mashang.domain.Student;
import com.mashang.util.ResultView;
import com.mashang.vo.student.StudentPageView;
import com.mashang.vo.student.StudentQueryVo;
import com.mashang.vo.student.StudentView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/student1/list")
public class StudentListServlet extends HttpServlet {

    public static List<Student> stuList = new ArrayList<Student>();

    static {
        Student studentView = new Student(20, 1100l, "2022-11-12 12:13:15", "张三", "18888888", "备注", "1", 1l, "3100");
        stuList.add(studentView);
        Student studentView2 = new Student(21, 1101l, "2023-11-12 12:13:15", "李四", "17777777", "备注2", "2", 2l, "3101");
        stuList.add(studentView2);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String studentNo = req.getParameter("studentNo");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
//        System.out.println("name=" + name + " studentNo=" + studentNo);
//        System.out.println("StudentListServlet.doGet");

        ResultView resultView = new ResultView();
//        resultView.setTotal(20);

//        List<StudentView> stuList = new ArrayList();
//        StudentView studentView = new StudentView(20, 1001l, "高一二班", "2022-11-12 12:13:15", "张三", "18888888", "备注", "1", 1l, "3100");
//        stuList.add(studentView);
//        StudentView studentView2 = new StudentView(21, 1001l, "高一一班", "2023-11-12 12:13:15", "李四", "17777777", "备注2", "2", 2l, "3101");
//        stuList.add(studentView2);

        StudentService studentService = new StudentService();
        StudentQueryVo queryVo = new StudentQueryVo();
        queryVo.setName(name);
        queryVo.setStudentNo(studentNo);
        PageVo pageVo = new PageVo();
        pageVo.setPageSize(Integer.parseInt(pageSize));
        pageVo.setPageNum(Integer.parseInt(pageNum));
        queryVo.setPageVo(pageVo);
        StudentPageView pageView = studentService.selectList(queryVo);

        resultView.setTotal(Integer.parseInt(pageView.getTotal() + ""));
        resultView.setRows(pageView.getStudentViewList());

        //json工具  对象-》json格式的字符串    json格式的字符串转成对象
        ObjectMapper objectMapper = new ObjectMapper();
        //将对象转成json格式的字符串
        String jsonStr = objectMapper.writeValueAsString(resultView);

//        String str = "{\"code\":200,\"msg\":\"操作成功!\",\"rows\":[{\"age\":18,\"name\":\"张三\"},{\"age\":20,\"name\":\"李四\"}],\"total\":10}";

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(jsonStr);
    }
}
